/*填充div，显示周历*/
function showCalendarData(divClass, date) {

	var dateObj = new Date(date);

	var _nowDayOfWeek = dateObj.getDay();
	var _nowDay = dateObj.getDate();
	var _weekStartDate = _nowDay - _nowDayOfWeek;
	var _year = dateObj.getFullYear();
	var _month = dateObj.getMonth() + 1;
	var _dateStr = getDateStr(dateObj);

	// 设置表格中的日期数据
	var _tables = $("." + divClass);

	_tables.each(function(ids, u) {
		var _table = $(u);
		_table.html("");
		_table.addClass("calendar-table");
		_table.attr("data-yearMouth", _year + "/" + _month);
		_table.attr("data-weekStartDate", _year + "/" + _month + "/" + _weekStartDate);
		var _tdsStr = "<tr>" +
			"<td><div></div></td>" +
			"<td><div></div></td>" +
			"<td><div></div></td>" +
			"<td><div></div></td>" +
			"<td><div></div></td>" +
			"<td><div></div></td>" +
			"<td><div></div></td>" +
			"</tr>";

		var _tdsObj = $(_tdsStr);
		_tdsObj.appendTo(_table);

		var _tds = _table.find("td");

		for(var i = 0; i < _tds.length; i++) {
			var tdOne = $(_tds[i]);
			var tdDiv = tdOne.find("div");

			var _thisDay = new Date(_year, _month - 1, i + _weekStartDate); //今天的对象

			var _thisDayStr = getDateStr(_thisDay); //今天的字符串表示形式

			tdDiv.attr("id", "dateDiv" + _thisDayStr);
			tdDiv.html(_thisDay.getDate());
			tdDiv.attr('data', _thisDayStr);
			tdDiv.addClass("thisIsDateButton");
			tdDiv.addClass("currentMonth");

		}
	});

}
/*日期转化为字符串， 4位年+2位月+2位日	*/
function getDateStr(date) {
	var _year = date.getFullYear();
	var _month = date.getMonth() + 1; // 月从0开始计数
	var _d = date.getDate();

	_month = (_month > 9) ? ("" + _month) : ("0" + _month);
	_d = (_d > 9) ? ("" + _d) : ("0" + _d);
	return _year + _month + _d;
}
/*日期转化为字符串， 2017-09-18	*/
function getDateStr2(date) {
	var _year = date.getFullYear();
	var _month = date.getMonth() + 1; // 月从0开始计数
	var _d = date.getDate();

	_month = (_month > 9) ? ("" + _month) : ("0" + _month);
	_d = (_d > 9) ? ("" + _d) : ("0" + _d);
	return _year +"-"+ _month +"-"+ _d;
}
/*日期转化为字符串，2017-07	*/
function getShortDateStr(date) {

	var _year = date.getFullYear();
	var _month = date.getMonth() + 1; // 月从0开始计数

	_month = (_month > 9) ? ("" + _month) : ("0" + _month);
	return _year + "-" + _month;
}
/*日期转化为字符串，2017/7	*/
function getShortDateStr2(date) {

	var _year = date.getFullYear();
	var _month = date.getMonth() + 1; // 月从0开始计数

	return _year + "/" + _month;
}

/*获取星期中文表示形式*/
var dayNames = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");

function getWeekDayStr(date) {
	return dayNames[date.getDay()];
}
/*从服务器获取某月的课程安排情况，并设置月历
 因API不符合预期
 api将所有数据全部返回，故在此做本地化处理.*/
var UserLesson;
//获取月份的课程数据，并展示到日历上
function showMouthLesson(date) {
	var dataSrt = getDateStr2(date);
	$.zcyget("getUserLessonWeek", {
		date: dataSrt
	}, function(d) {
		UserLesson = d.data;
		$.each(UserLesson, function(i, u) {
			var dateStr = u.date.replace(/-/g, "");
			var divs = $("div[data=" + dateStr + "]");
			$.each(divs, function(k, n) {
				$(n).addClass("tableHaveLession");
			});
		});
	});
}

function showUserInfo2() {
	//获取金币，播放时间，课文数并设置显示
	var coinsNumObj = $("#coinsNum");
	var lessonNumObj = $("#lessonNum");
	var timeNumObj = $("#timeNum");
	var rankNumObj = $("#rankNum");
	$.zcyget("getUserRecord", null, function(d) {
		coinsNumObj.text(d.data.coins);
		lessonNumObj.text(d.data.lesson_count);
		timeNumObj.text(d.data.time_count);
		rankNumObj.text(d.data.ranking);
	});

	showUserInfo('userInfo');
}

function initCalendarTable() {

	var _dateObj = new Date(); //今天
	var _nowDayOfWeek = _dateObj.getDay(); //今天是周几
	var _nowDay = _dateObj.getDate(); //今天的日期，几号
	var _weekStartDate = _nowDay - _nowDayOfWeek; //本周日（第一天）是几号

	var nowDateObj = new Date().setDate(_weekStartDate); //第一次运行时的当前周的第一天的Obj
	nowDateObj = new Date(nowDateObj);
	showMouthLesson(nowDateObj);
	showCalendarData('calendarTable0', nowDateObj); //卡1 初始显示 当前月

	showCalendarData('calendarTable1', nowDateObj.setDate(_weekStartDate + 7));
	showCalendarData('calendarTable2', nowDateObj.setDate(_weekStartDate - 7));

	nowYearMouthObj.html(getShortDateStr2(nowDateObj));

	todayWeekDayObj.html(getWeekDayStr(nowDateObj));
	todayDateObj.html(nowDateObj.getDate());
	//获取月份的课程数据，并展示到日历上


	document.querySelector('.mui-slider').addEventListener('slide', function(event) {

		var obj1 = $(".mui-slider-item")[event.detail.slideNumber + 1];
		var obj2 = $(obj1).find("table");
		var nowDateStr = obj2.attr("data-yearMouth");
	
		nowYearMouthObj.html(nowDateStr);

		var weekStartDate = obj2.attr("data-weekStartDate");
		var weekStartDateObj = new Date(weekStartDate);
	showMouthLesson(weekStartDate);
	
		if(event.detail.slideNumber === 0) {

			showCalendarData('calendarTable1', weekStartDateObj.setDate(weekStartDateObj.getDate() + 7));
			showCalendarData('calendarTable2', weekStartDateObj.setDate(weekStartDateObj.getDate() - 14));
		} else if(event.detail.slideNumber === 1) { //切换到卡2 calendarTable

			showCalendarData('calendarTable0', weekStartDateObj.setDate(weekStartDateObj.getDate() - 7));
			showCalendarData('calendarTable2', weekStartDateObj.setDate(weekStartDateObj.getDate() + 14));
		} else if(event.detail.slideNumber === 2) { //切换到卡3 calendarTable2
			showCalendarData('calendarTable0', weekStartDateObj.setDate(weekStartDateObj.getDate() + 7)); //卡1 初始显示 当前月
			showCalendarData('calendarTable1', weekStartDateObj.setDate(weekStartDateObj.getDate() - 14));
		}
		bindDateButton();
	});
	bindDateButton();

}

function bindDateButton() {
	var nowCheckObj;
	$(".thisIsDateButton").on("tap", function(e) {
		if(nowCheckObj != undefined) nowCheckObj.removeClass("tableClicked");

		var obj = $(e.currentTarget);
		var dataStr = obj.attr("data");

		nowCheckObj = obj;
		obj.addClass("tableClicked");
		showDayLessonTime(obj.attr("data"));
	});
}

function initImgBtn() {
	$("#imgBtn1").on("tap", function(e) {
		var obj = $(e.currentTarget);
		var type = obj.attr("data-type");
		switch(type) {
			case "1":
				jiDe()
				break;
			case "2":
				zhengQue()
				break;
			default:
				break;
		}
	});
	$("#imgBtn2").on("tap", function(e) {
		var obj = $(e.currentTarget);
		var type = obj.attr("data-type");
		switch(type) {
			case "1":
				buJiDe()
				break;
			case "2":
				buZhengQue()
				break;
			default:
				break;
		}
	});
}
var nowStudyId = "0";
var nowStudyType = "sentence";

function initStudyTypeBtn() {
	$("#studyTypeSentence").on("tap", function(e) {
		$("#studyTypeWord").removeClass("studyTypeSelected")
		$("#studyTypeSentence").addClass("studyTypeSelected")
		nowStudyType = "sentence";
		showLessonContents();
	});
	$("#studyTypeWord").on("tap", function(e) {
		$("#studyTypeSentence").removeClass("studyTypeSelected")
		$("#studyTypeWord").addClass("studyTypeSelected")
		nowStudyType = "word";
		showLessonContents();
	});
}

function showDayLessonTime(date) {
	if(UserLesson == undefined) return false;
	var topPx = 3;
	var parentDivObj = $("#dayTimeDiv");
	parentDivObj.html("");
	var labelStr = "<label></label>"
	var divObj = $("#dayTimeDiv");
	//排个序。
	UserLesson.sort(function(a, b) {
		return parseInt(a.start.replace(/-/g, ''), 10) - parseInt(b.start.replace(/-/g, ''), 10); //升序
		//return parseInt(b._id.replace(/-/g, ''), 10) - parseInt(a._id.replace(/-/g, ''), 10);//降序
	});
	$.each(UserLesson, function(i, u) {
		if(u.date.replace(/-/g, "") == date) {
			var labelObj = $(labelStr);
			labelObj.addClass("timeDiv");
			labelObj.css("top", topPx + "%");
			topPx += 7;
			labelObj.html(u.start.substr(0, 5));
			labelObj.attr("data-id", u.id);
			labelObj.appendTo(parentDivObj);
		}
	});
	var nowTimeCheckObj;
	$(".timeDiv").on("tap", function(e) {
		if(nowTimeCheckObj != undefined) nowTimeCheckObj.removeClass("timeDivChecked");
		var obj = $(e.currentTarget);
		nowTimeCheckObj = obj;
		obj.addClass("timeDivChecked");
		nowStudyId = obj.attr("data-id");
		showLessonContents();
	});
}

function showLessonContents() {
	if(nowStudyType === "sentence") {
		showLessonSentence(nowStudyId);
		return true;
	}
	showLessonWord(nowStudyId);
}

function showLessonSentence(id) {
	var num = 1;
	var labelStr = "<label></label>"
	var brObj = $("<br/>");
	var divStr = "<div></div>";
	var spanStr = "<span></span>";
	var lessonContentsDiv = $("#lessonContentsDiv");
	lessonContentsDiv.html("");
	$.zcyget("testPlay", {
		lesson: id,
		type: "sentence"
	}, function(d) {
		$.each(d.data, function(i, u) {
			var divObj = $(divStr);

			var labelObj = $(labelStr);
			labelObj.addClass("sentenceLabel");
			labelObj.attr("id", "sentence_label_" + num);
			labelObj.html(num + ".");

			var spanObj = $(spanStr);
			spanObj.attr("id", "sentence_span_" + num);
			spanObj.attr("data-newWord", u.word);
			spanObj.attr("data-mp3Url", u.sentence_audio);
			spanObj.attr("data-mysqlId", u.id);
			spanObj.html(u.sentence);

			spanObj.appendTo(labelObj);
			var meaningLabelObj = $(labelStr);
			meaningLabelObj.attr("id", "sentence_meaning_" + num);
			meaningLabelObj.html(u.word_meaning);
			meaningLabelObj.addClass("wordMeaningLabel");
			meaningLabelObj.hide();

			labelObj.appendTo(divObj);
			brObj.clone().appendTo(divObj);
			meaningLabelObj.appendTo(divObj);
			divObj.appendTo(lessonContentsDiv);
			num += 1;
		});
		startStudy(1);
	});

}

function showLessonWord(id) {
	var num = 1;
	var labelStr = "<label></label>"
	var brObj = $("<br/>");
	var divStr = "<div></div>";
	var spanStr = "<span></span>";
	var lessonContentsDiv = $("#lessonContentsDiv");
	lessonContentsDiv.html("");
	$.zcyget("testPlay", {
		lesson: id,
		type: "word"
	}, function(d) {
		$.each(d.data, function(i, u) {
			var divObj = $(divStr);

			var labelObj = $(labelStr);
			labelObj.addClass("wordLabel");
			labelObj.attr("id", "word_label_" + num);
			labelObj.html(num + ".");

			var spanObj = $(spanStr);
			spanObj.attr("id", "word_span_" + num);
			spanObj.attr("data-newWord", u.word);
			spanObj.attr("data-mp3Url", u.word_audio);
			spanObj.attr("data-mysqlId", u.id);
			spanObj.html(u.word);

			spanObj.appendTo(labelObj);
			var meaningLabelObj = $(labelStr);
			meaningLabelObj.attr("id", "word_meaning_" + num);
			meaningLabelObj.html(u.word_meaning);
			meaningLabelObj.addClass("wordMeaningLabel");
			meaningLabelObj.hide();

			labelObj.appendTo(divObj);
			brObj.clone().appendTo(divObj);
			meaningLabelObj.appendTo(divObj);
			divObj.appendTo(lessonContentsDiv);
			num += 1;
		});
		startStudy(1);
	});

}

function showImg(type) {
	var imgBtn1Obj = $("#imgBtn1");
	var imgBtn2Obj = $("#imgBtn2");
	imgBtn1Obj.attr("data-type", type);
	imgBtn2Obj.attr("data-type", type);
	switch(type) {
		case 1:
			imgBtn1Obj.attr("src", "images/测试_播放/内容显示/记得图标.png");
			imgBtn2Obj.attr("src", "images/测试_播放/内容显示/不记得图标.png");
			break;
		case 2:
			imgBtn1Obj.attr("src", "images/测试_播放/内容显示/正确图标.png");
			imgBtn2Obj.attr("src", "images/测试_播放/内容显示/不正确按钮.png");
			break;
		default:
			imgBtn1Obj.hide();
			imgBtn2Obj.hide();
			break;
	}
}
var nowStudyId = 1;
var remeberNum = 0;

function startStudy(id) {
	nowStudyId = id;
	var nowStudySpanObj = $("#" + nowStudyType + "_span_" + id);
	if(nowStudySpanObj.length == 0) {
		alert("共测试" + (nowStudyId - 1) + "条语句，正确" + remeberNum + "条，错误" + (nowStudyId - 1 - remeberNum) + "条！");
		return false;
	}
	nowStudySpanObj.css("background-color", "#CACACA");
	playMp3(nowStudySpanObj.attr("data-mp3Url"));
	showImg(1);
}

function jiDe() {
	stopPlayMp3();
	var nowStudySpanObj = $("#" + nowStudyType + "_span_" + nowStudyId);
	var nowStudyMeaningObj = $("#" + nowStudyType + "_meaning_" + nowStudyId);

	nowStudySpanObj.css("background-color", "#FFFFFF");
	var newWord = nowStudySpanObj.attr("data-newWord");
	var htmlStr = nowStudySpanObj.html().replace(newWord, "<span class=newWords>" + newWord + "</span>");
	nowStudySpanObj.html(htmlStr);

	nowStudyMeaningObj.show();

	showImg(2);
	//var sectenceStr = u.sentence
}

function buJiDe() {
	stopPlayMp3();
	var nowStudySpanObj = $("#" + nowStudyType + "_span_" + nowStudyId);
	var nowStudyMeaningObj = $("#" + nowStudyType + "_meaning_" + nowStudyId);

	nowStudySpanObj.css("background-color", "#FFFFFF");
	var newWord = nowStudySpanObj.attr("data-newWord");
	var htmlStr = nowStudySpanObj.html().replace(newWord, "<span class=newWords>" + newWord + "</span>");
	nowStudySpanObj.html(htmlStr);

	nowStudyMeaningObj.show();

	buZhengQue();
}

function zhengQue() {
	var nowStudySpanObj = $("#" + nowStudyType + "_span_" + nowStudyId);
	if(nowStudyType === "sentence") {
		$.zcypost("addRememberSentences", {
			"sentences[]": nowStudySpanObj.attr("data-mysqlId")
		}, function(d) {

		});
	} else {
		$.zcypost("addRememberWords", {
			"Word[]": nowStudySpanObj.attr("data-mysqlId")
		}, function(d) {

		});
	}

	remeberNum += 1;
	startStudy(nowStudyId + 1);
}

function buZhengQue() {
	var nowStudySpanObj = $("#" + nowStudyType + "_span_" + nowStudyId);
	if(nowStudyType === "sentence") {
		$.zcypost("addHardSentences", {
			"sentences[]": nowStudySpanObj.attr("data-mysqlId")
		}, function(d) {

		});
	} else {
		$.zcypost("addHardWords", {
			"Word[]": nowStudySpanObj.attr("data-mysqlId")
		}, function(d) {

		});
	}

	startStudy(nowStudyId + 1);
}